package com.mengjiahui.auduoplayer.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.mengjiahui.auduoplayer.bean.Music;
import com.mengjiahui.auduoplayer.R;

import java.io.IOException;
import java.util.List;

/**
 * @author Meng
 * @data 2020/5/17
 */
public class NativeMusicAdapter extends RecyclerView.Adapter<NativeMusicAdapter.ViewHolder> {
    private Context context;
    private List<Music> musicList;
    private OnItemClickListener onItemClickListener;

    public NativeMusicAdapter(List<Music> musicList){
        this.musicList=musicList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context=parent.getContext();
        View view= LayoutInflater.from(context).inflate(R.layout.item_native_music,parent,false);
        ViewHolder viewHolder=new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Music music=musicList.get(position);
//        holder.imageView.setImageBitmap(music.getMusicImg());
        holder.MusicNameTextView.setText(music.getMusicName());
        holder.MusicAlbumAuthor.setText(music.getAuthor());
        holder.itemView.setOnClickListener(v->{
            try {
                onItemClickListener.touch(music);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    @Override
    public int getItemCount() {
        return musicList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView imageView;
        private TextView MusicNameTextView;
        private TextView MusicAlbumAuthor;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            this.imageView=itemView.findViewById(R.id.imageViewNativeMucisIcon);
            this.MusicNameTextView=itemView.findViewById(R.id.textViewMusicName);
            this.MusicAlbumAuthor=itemView.findViewById(R.id.textViewMusicAuthorAlbum);
        }
    }

    public interface OnItemClickListener{
        void touch(Music music) throws IOException;
    }
    public void setListener(OnItemClickListener onItemClickListener) {

        this.onItemClickListener = onItemClickListener;
    }
}
